package site.lanmushan.framework.configure.mvc;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import site.lanmushan.framework.configure.properties.PathPrefixProperties;
import site.lanmushan.framework.configure.properties.PathPrefixProperty;

/**
 * @author Administrator
 */
@Slf4j
@Configuration
public class PathPrefixConfigure implements WebMvcConfigurer {
    @Autowired
    PathPrefixProperties pathPrefixProperties;

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        if (pathPrefixProperties == null || pathPrefixProperties.getPrefixs() == null || pathPrefixProperties.getPrefixs().size() == 0) {

            return;
        }
        for (PathPrefixProperty prefixProperty : pathPrefixProperties.getPrefixs()) {
            log.info("添加API前缀:{} {}",prefixProperty.getPrefixPath(),prefixProperty.getBasePackage());
            configurer.addPathPrefix(prefixProperty.getPrefixPath(), c -> c.getPackage().getName().startsWith(prefixProperty.getBasePackage()));
        }

    }
}
